<template>
  <div class="show-main">
    <el-scrollbar style="height:100%">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form :inline="true" :model="searchForm">
            <el-form-item>
              <el-button type="primary" @click.stop="reload">刷新</el-button>
              <el-button type="success" @click.stop="add({level:-1,id:0})">添加根权限</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-alert title="注：可以右键展开或关闭下方权限树" type="warning" size="mini"></el-alert>
        <el-tree :data="table" node-key="id" ref="tree" size="small" highlight-current accordion :props="defaultProps"
          @node-contextmenu="nodeContextmenu" :expand-on-click-node="false" default-expand-all>
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <span>
              <el-button v-if="data.level <=1" type="text" @click="() => show(data)">
                添加子权限
              </el-button>
              <el-button type="text" @click="() => detail(data)">
                查看详情
              </el-button>
              <el-button type="text" @click="() => edit(data)">
                编辑
              </el-button>
              <el-button v-if="data.level <=1" type="text" @click="() => add(data)">
                添加子权限
              </el-button>
              <el-button type="text" @click="() => remove(data)">
                删除
              </el-button>
            </span>
          </span>
        </el-tree>
      </el-card>
      <el-dialog title="权限详情" :visible.sync="detailVisible" width="700px">
        <el-row>
          <el-col :span="12">ID: {{detaiForm.id}}</el-col>
          <el-col :span="12">PID: {{detaiForm.pid}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">名称: {{detaiForm.name}}</el-col>
          <el-col :span="12">icon: {{detaiForm.icon}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">接口: {{detaiForm.api}}</el-col>
          <el-col :span="12">前端路由: {{detaiForm.router}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">前端组件: {{detaiForm.component}}</el-col>
          <el-col :span="12">权限等级: {{detaiForm.level}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">创建时间: {{detaiForm.create_time|formatDate}}</el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="detailVisible = false" size="small">关闭</el-button>
        </span>
      </el-dialog>
      <el-dialog title="权限编辑" :visible.sync="editVisible" :before-close="handleClose('editForm')">
        <el-form ref="editForm" :model="editForm" label-width="80px" size="small" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
          <el-form-item v-if="editForm.level == 0" label="icon" prop="icon">
            <el-input v-model="editForm.icon"></el-input>
          </el-form-item>
          <el-form-item v-if="editForm.level == 1" label="前端路由" prop="router">
            <el-input v-model="editForm.router"></el-input>
          </el-form-item>
          <el-form-item v-if="editForm.level == 1" label="前端组件" prop="component">
            <el-input v-model="editForm.component"></el-input>
          </el-form-item>
          <el-form-item v-if="editForm.level == 2" label="接口" prop="api">
            <el-input v-model="editForm.api"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="editForm.sort" :precision="0" :step="1" :max="255"></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('editVisible', 'editForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('editVisible','editForm')" size="small">编 辑</el-button>
        </span>
      </el-dialog>
      <el-dialog :title="title" :visible.sync="addVisible" :before-close="handleClose('addForm')">
        <el-form ref="addForm" :model="addForm" label-width="80px" size="small" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item v-if="addForm.level == -1" label="icon" prop="icon">
            <el-input v-model="addForm.icon"></el-input>
          </el-form-item>
          <el-form-item v-if="addForm.level == 0" label="前端路由" prop="router">
            <el-input v-model="addForm.router"></el-input>
          </el-form-item>
          <el-form-item v-if="addForm.level == 0" label="前端组件" prop="component">
            <el-input v-model="addForm.component"></el-input>
          </el-form-item>
          <el-form-item v-if="addForm.level == 1" label="接口" prop="api">
            <el-input v-model="addForm.api"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="addForm.sort" :precision="0" :step="1" :max="255"></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('addVisible', 'addForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('addVisible','addForm')" size="small">添 加</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import { getAuthList, getAuthOnly, editAuth, addAuth, delAuth } from '@/network/api.js'

export default {
  name: 'auths',
  data() {
    return {
      searchForm: {},
      table: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      title: '',
      detaiForm: {
        id: 0,
        pid: 0,
        name: '',
        icon: '',
        api: '',
        router: '',
        component: '',
        level: 0,
        create_time: 0
      },
      editForm: {
        id: 0,
        pid: 0,
        name: '',
        icon: '',
        api: '',
        router: '',
        component: '',
        sort: 1
      },
      addForm: {
        level: -1,
        pid: 0,
        name: '',
        icon: '',
        api: '',
        router: '',
        component: '',
        sort: 1
      },
      detailVisible: false, // 详情
      editVisible: false, // 编辑
      addVisible: false, // 编辑
      rules: {
        name: [{ required: true, message: '权限名称必须', trigger: 'blur' }],
        icon: [{ required: true, message: '权限icon必须', trigger: 'blur' }],
        router: [{ required: true, message: '前端路由必须', trigger: 'blur' }],
        component: [{ required: true, message: '前端组件必须', trigger: 'blur' }],
        api: [{ required: true, message: '接口必须', trigger: 'blur' }],
        sort: [{ required: true, message: '排序必须', trigger: 'blur' }]
      },
      selectId: []
    }
  },
  created() {
    this.search()
  },
  computed: {},
  methods: {
    reload() {
      this.search()
    },
    async search() {
      try {
        const rtn = await getAuthList(this.searchForm)
        this.table = rtn
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 右键展开关闭节点
    nodeContextmenu(event, data, node, obj) {
      node.expanded = !node.expanded
    },
    // 详情
    async detail(data) {
      try {
        const rtn = await getAuthOnly({ id: data.id })
        this.detaiForm = rtn
        this.detailVisible = true
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 编辑
    async edit(data) {
      try {
        const rtn = await getAuthOnly({ id: data.id })
        this.editVisible = true
        this.$nextTick(() => {
          this.editForm = rtn
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加
    add(data) {
      this.addForm.level = data.level
      this.addForm.pid = data.id
      if (data.level === -1) {
        this.title = '根权限添加'
      } else {
        this.title = `${data.name}---子权限添加`
      }
      this.addVisible = true
    },
    // 删除
    remove(data) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await delAuth({ id: data.id })
            this.search()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(e => {
          this.$message.warning('您已取消操作')
        })
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose(formName) {
      return done => {
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close(visibleName, formName) {
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send(visibleName, formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          switch (formName) {
            case 'editForm': // 编辑权限
              try {
                await editAuth(this.editForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'addForm': // 添加权限
              try {
                await addAuth(this.addForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          this.$message.error('请检查表单')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
